// The contents of this file are subject to the Mozilla Public License Version
// 1.1
//(the "License"); you may not use this file except in compliance with the
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
//
//Software distributed under the License is distributed on an "AS IS" basis,
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
//for the specific language governing rights and
//limitations under the License.
//
//The Original Code is "The Columba Project"
//
//The Initial Developers of the Original Code are Frederik Dietz and Timo
// Stich.
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
//
//All Rights Reserved.
package org.columba.mail.filter;

import org.columba.api.command.ICommand;
import org.columba.api.plugin.IExtension;
import org.columba.api.plugin.IExtensionHandler;
import org.columba.core.command.CompoundCommand;
import org.columba.core.filter.AbstractFilterAction;
import org.columba.core.filter.Filter;
import org.columba.core.filter.FilterAction;
import org.columba.core.filter.FilterActionList;
import org.columba.core.filter.IFilter;
import org.columba.core.filter.IFilterAction;
import org.columba.core.filter.IFilterActionList;
import org.columba.core.folder.api.IFolder;
import org.columba.core.plugin.PluginManager;
import org.columba.mail.folder.IMailbox;

/**
 * @author fdietz
 * 
 */
public class FilterCompoundCommand extends CompoundCommand {

	/**
	 * 
	 */
	public FilterCompoundCommand(IFilter filter, IFolder sourceFolder,
			Object[] uids) throws Exception {
		super();

		// get plugin handler for filter actions
		IExtensionHandler pluginHandler =  PluginManager
				.getInstance().getExtensionHandler("org.columba.mail.filteraction");

		// get list of all filter actions
		IFilterActionList list = filter.getFilterActionList();

		for (int i = 0; i < list.getChildCount(); i++) {
			// interate through all filter actions
			IFilterAction action = list.get(i);

			// name is used to load plugin
			String name = action.getAction();
			AbstractFilterAction instance = null;

			// try to get instance of FilterAction
			try {
				IExtension extension = pluginHandler.getExtension(name);

				instance = (AbstractFilterAction) extension
						.instanciateExtension(null);
			} catch (Exception ex) {
				ex.printStackTrace();
			}

			// retrieve Command of filter action
			ICommand command = instance.getCommand(action,
					(IMailbox) sourceFolder, uids);

			// add command to CompoundCommand
			if (command != null) {
				add(command);
			}
		}
	}

}